//
//  SerialMapCell.m
//  XPhoto
//
//  Created by baboy on 2019/7/5.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "PhotoMapCell.h"
#import "PhotoRecord.h"
#import "BKAnnotation.h"
#import "BKAnnotationView.h"

@interface PhotoMapCell()
@property(nonatomic, strong) NSMutableArray *itemViews;
@end
@implementation PhotoMapCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setItems:(NSMutableArray *)items{
    if (self.itemViews) {
        [self.itemViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    _items = [NSMutableArray arrayWithArray:items];
    MKMapRect visibleRect = MKMapRectNull;
    MKCoordinateSpan theSpan;
    //地图的范围 越小越精确
    theSpan.latitudeDelta = 0.01;
    theSpan.longitudeDelta = 0.01;
    MKCoordinateRegion visibleRegion;
    visibleRegion.span = theSpan;
    
    for (NSInteger i = 0; i<items.count; i++) {
        PhotoRecord *item = items[i];
        if (!item.lat || !item.lng) {
            continue;
        }
        MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
        anno.coordinate = CLLocationCoordinate2DMake([item.lat doubleValue], [item.lng doubleValue]);
        anno.title = item.addr;
        anno.subtitle = item.content;
//        anno.userInfo = item;
        
        [self.mapView addAnnotation:anno];
        MKMapPoint p = MKMapPointForCoordinate(anno.coordinate);
        MKMapRect rect = MKMapRectMake(p.x, p.y, 0, 0);
        if ( MKMapRectIsNull(visibleRect)) {
            visibleRect = rect;
            
//            visibleRegion.center = CLLocationCoordinate2DMake(39.922932f, 116.445100f);
//            [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(39.922932f, 116.445100f) animated:YES];
//            [self.mapView setRegion:visibleRegion];
            continue;
        }
        visibleRect = MKMapRectUnion(visibleRect, rect);
    }
    [self.mapView setVisibleMapRect:visibleRect];
//    [self.mapView setVisibleMapRect:visibleRect animated:YES];
}
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    if([annotation isKindOfClass:[MKPointAnnotation class]]){
//        PhotoRecord *photoRecord = ((BKAnnotation*)annotation).userInfo;
        static NSString *ANNO_VIEW_ID = @"BKAnnotationView";
        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ANNO_VIEW_ID];
        if (!annotationView) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ANNO_VIEW_ID];
            annotationView.opaque = NO;
            [annotationView setDraggable:YES];
        }
        annotationView.annotation = annotation;
        annotationView.canShowCallout = YES;
//        [annotationView setImageURLString:photoRecord.items.firstObject.url];
        annotationView.image = [UIImage imageNamed:@"image"];
        return annotationView;
//        annotationView.detailCalloutAccessoryView.backgroundColor = [UIColor brownColor];
        annotationView.rightCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        annotationView.rightCalloutAccessoryView.backgroundColor = [UIColor redColor];
        annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        annotationView.leftCalloutAccessoryView.backgroundColor = [UIColor blueColor];
        
        UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [detailLabel setText:@"xxxxx"];
        detailLabel.translatesAutoresizingMaskIntoConstraints = false;
        detailLabel.backgroundColor = [UIColor brownColor];
        detailLabel.backgroundColor = [UIColor blackColor];
        detailLabel.opaque = NO;
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
        annotationView.detailCalloutAccessoryView = detailLabel;
        
//        annotationView.backgroundView.backgroundColor = [UIColor redColor];
//        annotationView.backgroundView.layer.cornerRadius = 5;
//        annotationView.backgroundView.clipsToBounds = YES;
        return annotationView;
    }
    return nil;
}

- (CGFloat)heightWithWidth:(CGFloat)width{
    CGFloat h = self.bounds.size.height;
    return h;
}
@end
